#import "YLMoneyUtil.h"

@implementation YLMoneyUtil

+(NSString*)round:(NSString*)price {
    if (price == nil) {
        return @"0";
    }
    NSDecimalNumber *numPrice = [[NSDecimalNumber alloc]initWithString:price];
    NSDecimalNumberHandler* handle = [NSDecimalNumberHandler
                                                decimalNumberHandlerWithRoundingMode:NSRoundBankers
                                                scale:2
                                                raiseOnExactness:NO
                                                raiseOnOverflow:NO
                                                raiseOnUnderflow:NO
                                                raiseOnDivideByZero:NO];
    NSDecimalNumber *res = [numPrice decimalNumberByRoundingAccordingToBehavior:handle];
    return [NSString stringWithFormat:@"%@",res];
}

+(NSString*)add:(NSString*)add1 add2:(NSString*)add2 {
    NSDecimalNumber *addNum1 = [[NSDecimalNumber alloc]initWithString:add1 ? add1  : @"0"];
    NSDecimalNumber *addNum2 = [[NSDecimalNumber alloc]initWithString:add2 ? add2  : @"0"];
    NSDecimalNumberHandler* handle = [NSDecimalNumberHandler
                                      decimalNumberHandlerWithRoundingMode:NSRoundBankers
                                      scale:2
                                      raiseOnExactness:NO
                                      raiseOnOverflow:NO
                                      raiseOnUnderflow:NO
                                      raiseOnDivideByZero:NO];
    NSDecimalNumber *res =  [addNum1 decimalNumberByAdding:addNum2 withBehavior:handle];
    return [NSString stringWithFormat:@"%@",res];
}

+(NSString*)mul:(NSString*)mul1 mul2:(NSString*)mul2 {
    NSDecimalNumber *mulNum1 = [[NSDecimalNumber alloc]initWithString:mul1 ? mul1  : @"0"];
    NSDecimalNumber *mulNum2 = [[NSDecimalNumber alloc]initWithString:mul2 ? mul2  : @"0"];
    NSDecimalNumberHandler* handle = [NSDecimalNumberHandler
                                      decimalNumberHandlerWithRoundingMode:NSRoundBankers
                                      scale:2
                                      raiseOnExactness:NO
                                      raiseOnOverflow:NO
                                      raiseOnUnderflow:NO
                                      raiseOnDivideByZero:NO];
    NSDecimalNumber *res =  [mulNum1 decimalNumberByMultiplyingBy:mulNum2 withBehavior:handle];
    return [NSString stringWithFormat:@"%@",res];
}

@end
